#!/usr/bin/env python
# -*- coding: utf-8 -*-

import dabo.biz
import datetime

class HoursBizobj(dabo.biz.dBizobj):
    def afterInit(self):
        self.DataSource = "hours"
        self.KeyField = "pkid"
        self.addFrom("hours")
        self.addJoin("clients",
            "hours.clientfk = clients.pkid")    # <---
        self.addField("hours.clientfk")
        self.addField("hours.hours")
        self.addField("hours.notes")
        self.addField("hours.billed")
        self.addField("hours.servicedate")
        self.addField("hours.pkid")
        self.addField("clients.clientname") # <---
        # Note: if you're using SQLite for your backend,  # <---
        # change the next line to:  # <---
        # self.addWhere("hours.billed = 0")  # <---
        self.addWhere("hours.billed = 0") # <---
        self.addOrderBy("servicedate asc")  # <---
        self.DefaultValues = {"servicedate": datetime.date.today().__str__}
        

    def validateRecord(self):
        """Returning anything other than an empty string from
        this method will prevent the data from being saved.
        """
        ret = ""
        # Add your business rules here. 
        if self.Record.clientfk == 0:
            ret = "You must select a client"
        return ret
